package com.aaron.demo1.test.megaeyes.action;


import cn.hutool.http.HttpRequest;
import com.aaron.demo1.test.megaeyes.model.PTZControl;
import com.aaron.demo1.test.megaeyes.service.ApiService;
import com.aaron.demo1.test.megaeyes.service.PTZControlService;
import com.aaron.demo1.test.megaeyes.service.impl.PTZControlServiceImpl;
import com.aaron.demo1.test.megaeyes.util.TableUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.aaron.demo1.test.megaeyes.service.impl.PTZControlServiceImpl.TIME_OUT_IN_MILLISECONDS;

/**
 * PTZ控制：全球眼云台全方位（左右/上下）移动及镜头变倍、变焦控制
 *
 * @author liurong
 * @date 2021/7/15 9:51
 * @version: v1.4.3 beta
 */
@RestController
@RequestMapping("/robot/pc/megaeyes")
public class PTZControlAction {

    @Autowired
    ApiService apiService;

    @Autowired
    PTZControlService ptzControlService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

//    @Autowired
//    PTZControlServiceImpl ptzControlService;


    String testToken;

    public static final String ACTION = "/videoService/ptz/";


    @GetMapping("/time")
    public String testTime() throws Exception{
        String token = apiService.getToken("xxx");
        Date start = new Date();

        this.testToken = token;
        String channelCode = "xxx";

        boolean flag = false;
        String url = "https://61.151.156.1:8282"+ ACTION + channelCode;
        String content = JSONObject.toJSONString(new PTZControl());
        while (flag) {
            String response = HttpRequest.put(url).header("X-Subject-Token",token).body(content).timeout(TIME_OUT_IN_MILLISECONDS).execute().body();
            if (StringUtils.isNotEmpty(response)) {
                flag = true;
            }
            Thread.sleep(1000*5);
        }
        Date end = new Date();


        return (end.getTime() - start.getTime()) + "";
    }

    @PostMapping("/move")
    public String move(String direction, String equipmentId,int pos) {

        int response = ptzControlService.move(direction, equipmentId,pos);
        return TableUtil.tojson(response);
    }

    @GetMapping
    public String getChannelCode(){
        List<Map<String, Object>> xxx = apiService.getChannelId(null, "xxx");
        Map<String, Object> map1 = xxx.get(0);

        String id = (String)map1.get("id");
        List<Map<String, Object>> x2 = apiService.getChannelId(id, "x2");
        Map<String, Object> m2 = x2.get(0);
        String id1 = (String)m2.get("id");
        List<Map<String, Object>> x3 = apiService.getChannelId(id1, "x3");
        return JSONObject.toJSONString(x3);
    }

    public String move2(String direction, String equipmentId,int pos) {
        int response = ptzControlService.move(direction, equipmentId,pos);
        return TableUtil.tojson(response);
    }

    @GetMapping("/test")
    public String tokenTime() {

        Date start = new Date();
        String token = apiService.getToken("xx");
        Date end = new Date();
        HashMap<String, String> res = new HashMap<>();
        res.put("time", (end.getTime() - start.getTime()) + " milliseconds");
        res.put("token", token);
        stringRedisTemplate.opsForValue().set("megaEyesTokenxxxx", token);
        return JSONObject.toJSONString(res);
        /**
         * 255
         * 502
         * 1519
         * 1604
         * 337
         * 247
         * 305
         * 299
         * 214
         * 407
         * 638
         * 292
         * 303
         * 1132
         * 660
         * 2854
         * 308
         * 306
         * 2108
         * 3353
         * 362
         * 323
         */
    }

    @GetMapping("/testRedis")
    public String tokenTime2() {

        Date start = new Date();
        String token = stringRedisTemplate.opsForValue().get("megaEyesTokenxxxx");
        Date end = new Date();
        HashMap<String, String> res = new HashMap<>();
        res.put("time", (end.getTime() - start.getTime()) + " milliseconds");
        res.put("token", token);
        return JSONObject.toJSONString(res);
        /*
            224
            1
            2
            3
            2
            1
            1
            1
            2
            3
         */
    }


}
